package com.example.memohehang.service.Impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.memohehang.common.R;
import com.example.memohehang.domain.User;
import com.example.memohehang.mapper.UserMapper;
import com.example.memohehang.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService
{

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public R login(User user)
    {
        // 判断用户账号是否正确
        User one = getOne(new QueryWrapper<User>()
                .eq("username", user.getUsername())
                .eq("password", user.getPassword())
        );

        if (one != null)
        {
            return R.success(200,"登录成功", one);
        }
        else
        {
            return R.error(405, "账号或密码错误");
        }
    }

    @Override
    public R register(User user)
    {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        System.out.println(user);
        User tempUsername = getOne(queryWrapper);
        // 先判断一下用户是否存在 存在就返回false
        if (tempUsername != null)
        {
            return R.error(405,"账户已存在");
        }
        else
        {
            //网上随机找的一个图片
            user.setAvatar("https://img2.baidu.com/it/u=3841326637,2519425910&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=501");
            // 反之加入
            save(user);
            return R.success(user);
        }
    }

    /**
     * 根据id查询用户
     * @param userid
     * @return
     */
    @Override
    public R selectUserById(Integer userid)
    {
        // 判断用户账号是否正确
        User one = getOne(new QueryWrapper<User>()
                .eq("id", userid)
        );

        if (one != null)
        {
            return R.success(200,"查询成功", one);
        }
        else
        {
            return R.error(405, "查询失败");
        }
    }
}


